package com.example.lt.mytest;


import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;

public class MainActivity extends AppCompatActivity {
    private SQLiteDatabase db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        db = openOrCreateDatabase("john1.db", SQLiteDatabase.OPEN_READWRITE, null);
    }

    public void open(View view) {

        String sql = "create table users(id integer primary key autoincrement,name text)";
        try {
            db.execSQL(sql);
        }catch (Exception e){

        }

    }

    public void insert(View view) {
        String sql = "insert into users (id,name) values(null,'psy')";
        db.execSQL(sql);
    }

    public void query(View view) {
        String sql = "select * from users";
        Cursor c = db.rawQuery(sql, null);
        while (c.moveToNext()) {
            Log.i("john", c.getInt(c.getColumnIndex("id")) + "");
            Log.i("john", c.getString(c.getColumnIndex("name")) + "");
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        db.close();
    }
}




